package com.ibbt.musicplayer.client.panels;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.ToggleButton;
import com.ibbt.musicplayer.client.LayoutResources;
import com.ibbt.musicplayer.client.service_interfaces.NowOnAirParserService;
import com.ibbt.musicplayer.client.service_interfaces.NowOnAirParserServiceAsync;
import com.ibbt.musicplayer.client.utils.NoaObject;
import com.ibbt.musicplayer.model.Radio;
import com.ibbt.musicplayer.model.RadioNoa;

public class RadioStationPanel extends AbsolutePanel {
	private Image imageRadiostation;
	private Image imageLiked;
	private Image imageUnliked;
	private Image imageFacebook;
	private ToggleButton buttonLike;
	private PushButton buttonFacebookShare;
	private Label labelFacebook;
	private Label labelZenderNaam;
	private Label labelNowOnAir;
	private Label labelAantalFavorieten;
	private String artiest = "Artiest";
	private String nummer = "Nummer";
	private int aantalfavorieten = 0;

	private final NowOnAirParserServiceAsync parserService = GWT
			.create(NowOnAirParserService.class);

	public RadioStationPanel(Radio radio) throws RequestException {
		// Stijl toevoegen
		this.addStyleName("radioframe");

		// Afbeelding van zender toevoegen
		imageRadiostation = new Image("../images/radiostations/"
				+ radio.getImage());
		imageRadiostation.addStyleName("zenderimg");
		this.add(imageRadiostation, 5, 5);

		// Label Nu op ZENDER toevoegen
		labelZenderNaam = new Label("Nu op " + radio.getName());
		labelZenderNaam.addStyleName("bigtext");
		this.add(labelZenderNaam, 6, 60);

		// Now On Air info abeelden
		showNowOnAirInfo(radio);

		// Like Button toevoegen
		imageLiked = new Image(LayoutResources.INSTANCE.liked());
		imageUnliked = new Image(LayoutResources.INSTANCE.unliked());
		imageUnliked.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				// favoriet aangevinkt!
			}
		});
		buttonLike = new ToggleButton(imageUnliked, imageLiked);
		buttonLike.addStyleName("likebutton");

		// Aantal favorieten label toevoegen
		labelAantalFavorieten = new Label("(" + aantalfavorieten + ")");
		labelAantalFavorieten.addStyleName("nowplaying");

		// Alles in balk in dockpanel en horizontal panel organiseren
		// om mooie layout te krijgen.
		DockPanel panel = new DockPanel();
		panel.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
		panel.add(labelNowOnAir, DockPanel.WEST);
		panel.addStyleName("labelonair");
		panel.setWidth("100%");
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.add(labelAantalFavorieten);
		hpanel.add(buttonLike);
		hpanel.setCellVerticalAlignment(buttonLike,
				HorizontalPanel.ALIGN_BOTTOM);
		hpanel.setCellVerticalAlignment(labelAantalFavorieten,
				HorizontalPanel.ALIGN_MIDDLE);
		panel.add(hpanel, DockPanel.EAST);
		panel.setCellHorizontalAlignment(hpanel, DockPanel.ALIGN_RIGHT);
		this.add(panel, 0, 85);

		// Delen op facebook knop toevoegen
		// labelFacebook = new Label("Delen via ");
		// labelFacebook.addStyleName("nowplaying");
		// imageFacebook = new
		// Image(LayoutResources.INSTANCE.share_via_facebook());
		// imageFacebook.addClickHandler(new ClickHandler() {
		// public void onClick(ClickEvent event) {
		// //publish on facebook!
		// }
		// });
		// buttonFacebookShare = new PushButton(imageFacebook);
		// this.add(labelFacebook,230,10);
		// this.add(buttonFacebookShare,290,5);
	}

	/**
	 * Haalt de Now On Air informatie van de huidige zender op
	 * en refresht deze om de 30seconden.
	 * @param radio
	 */
	private void showNowOnAirInfo(Radio radio) {
		final RadioNoa noa = radio.getRadioNoa();
		labelNowOnAir = new Label("Geen Info Beschikbaar");
		labelNowOnAir.addStyleName("nowplaying");
		Timer t = new Timer() {
			@Override
			public void run() {
				parserService.parse(noa.getUrl(), noa.getType(),
						new NowOnAirParserCallback());
			}
		};
		t.scheduleRepeating(30000); //30s
	}

	/*************************** CALLBACK CLASSES ************************/

	/** 
	 * Deze (asynchrone) callback klasse wordt gebruikt wanneer de NOA
	 * informatie opgehaald wordt van de server.
	 * */
	private class NowOnAirParserCallback implements AsyncCallback<NoaObject> {
		@Override
		public void onFailure(Throwable caught) {
			// TODO
			Window.alert("fout bij ophalen noa!");
			caught.printStackTrace();
		}

		@Override
		public void onSuccess(NoaObject result) {
			if (result.getStatus()) {
				setLabelNowOnAirText(result.getArtist() + " - "
						+ result.getSong());
			} else {
				setLabelNowOnAirText(result.getArtist());
			}
		}
	};
	
	/**
	 * Hulpfunctie die gebruikt wordt door bovenstaande callback klasse
	 * om de NOA informatie af te beelden in de speler.
	 * @param s
	 */
	public void setLabelNowOnAirText(String s) {
		this.labelNowOnAir.setText(s);
	}
}
